//
//  WKHTTPSessionManager.m
//  ourToken
//
//  Created by wakeup on 2018/6/14.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKHTTPSessionManager.h"

@implementation WKHTTPSessionManager

//登录
+ (void)loginWithPhoneNum:(NSString *)phoneNum password:(NSString *)password success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"mobile":phoneNum,@"password":password};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_Login dict:params success:success failure:failure];
}

//注册
+ (void)registerWithPhoneNum:(NSString *)phoneNum code:(NSString *)code password:(NSString *)password success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"mobile":phoneNum,@"code":code,@"password":password};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_Register dict:params success:success failure:failure];
}

//验证码
+ (void)getCodeWithPhoneNum:(NSString *)phoneNum success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"data"] = phoneNum;
    [self postWithUrl:URL_GetCode dict:params success:success failure:failure];
}

//绑定设备
+ (void)bindingDevicesWithIdentifier:(NSString *)identifier success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"device":identifier};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_BindingDevices dict:params success:success failure:failure];
}

//钱包列表
+ (void)getWalletListWithPageIndex:(int)pageIndex pageSize:(int)pageSize success:(successBlock)success failure:(errorBlock)failure {
    
    NSString *url = [NSString stringWithFormat:@"%@?pageIndex=%d&pageSize=%d",URL_WalletList,pageIndex,pageSize];
    
    [self getWithUrl:url dict:nil success:success failure:failure];
}

//获取钱包序列号
+ (void)getWalletSerialNumberSuccess:(successBlock)success failure:(errorBlock)failure {
    [self getWithUrl:URL_WalletSerialNumber dict:nil success:success failure:failure];
}

//创建钱包
+ (void)foundWalletWithCoinType:(NSString *)coinType name:(NSString *)name address:(NSString *)address serialNumber:(NSString *)serialNumber createType:(int)createType encryptedStr:(NSString *)encryptedStr success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"coinType":coinType,@"name":name,@"address":address,@"serialNumber":serialNumber,@"createType":@(createType),@"encryptedStr":encryptedStr};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_FoundWallet dict:params success:success failure:failure];
}

//删除钱包
+ (void)deleteWalletWithWalletId:(NSInteger)walletId success:(successBlock)success failure:(errorBlock)failure {
    
    NSString *url = [NSString stringWithFormat:@"%@%ld",URL_DeleteWallet,(long)walletId];
    [self deleteWithUrl:url dict:nil success:success failure:failure];
}

//钱包交易列表
+ (void)getWalletTransactionListWithWalletId:(NSInteger)walletId coinType:(NSString *)coinType pageIndex:(int)pageIndex pageSize:(int)pageSize success:(successBlock)success failure:(errorBlock)failure {
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld/%@?pageIndex=%d&pageSize=%d",URL_WalletTransactionList,walletId,coinType,pageIndex,pageSize];
    
    [self getWithUrl:url dict:nil success:success failure:failure];
}

//地址列表
+ (void)getAddressListWithPageIndex:(int)pageIndex pageSize:(int)pageSize success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"pageIndex":@(pageIndex),@"pageSize":@(pageSize)};
    [params setObject:dict forKey:@"data"];
    [self getWithUrl:URL_AddressList dict:params success:success failure:failure];
}

//添加地址
+ (void)addAddressWithCoinType:(NSString *)coinType name:(NSString *)name address:(NSString *)address success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"coinType":coinType,@"name":name,@"address":address};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_AddAddress dict:params success:success failure:failure];
}

//修改地址
+ (void)updateAddressWithAddressId:(NSInteger)addressId coinType:(NSString *)coinType name:(NSString *)name address:(NSString *)address success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"coinType":coinType,@"name":name,@"address":address};
    [params setObject:dict forKey:@"data"];
    NSString *url = [NSString stringWithFormat:@"%@%ld",URL_UpdateAddress,addressId];
    [self patchWithUrl:url dict:params success:success failure:failure];
}

//删除地址
+ (void)deleteAddressWithAddressId:(NSInteger)addressId success:(successBlock)success failure:(errorBlock)failure {
    
    NSString *url = [NSString stringWithFormat:@"%@%ld",URL_DeleteAddress,addressId];
    [self deleteWithUrl:url dict:nil success:success failure:failure];
}

//修改用户语言
+ (void)setLanguageType:(NSString *)languageType success:(successBlock)success failure:(errorBlock)failure {
    
    NSDictionary *params = @{@"data":languageType};
    [self patchWithUrl:URL_LanguageType dict:params success:success failure:failure];
}

//修改用户货币类型
+ (void)setCurrencyType:(NSString *)currencyType success:(successBlock)success failure:(errorBlock)failure {
    
    NSDictionary *params = @{@"data":currencyType};
    [self patchWithUrl:URL_CurrencyType dict:params success:success failure:failure];
}

//获取以太币交易费区间
+ (void)getEthFeesRangeSuccess:(successBlock)success failure:(errorBlock)failure {
    [self getWithUrl:URL_EthFeesRange dict:nil success:success failure:failure];
}

//构建交易
+ (void)buildTransactionWithWalletId:(NSInteger)walletId toAddress:(NSString *)toAddress amount:(NSString *)amount fee:(NSString *)fee success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"walletId":@(walletId),@"toAddress":toAddress,@"amount":amount,@"fee":fee};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_BuildTransaction dict:params success:success failure:failure];
}

//新增比特币/以太坊交易
+ (void)addTransactionWithCoinType:(NSString *)coinType walletId:(NSInteger)walletId toAddress:(NSString *)toAddress remark:(NSString *)remark txHex:(NSString *)txHex success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"coinType":coinType,@"walletId":@(walletId),@"toAddress":toAddress,@"remark":remark,@"txHex":txHex};
    [params setObject:dict forKey:@"data"];
    [self postWithUrl:URL_Transactions dict:params success:success failure:failure];
}

//交易列表
+ (void)getTransactionListWithPageIndex:(int)pageIndex pageSize:(int)pageSize success:(successBlock)success failure:(errorBlock)failure {
    
    NSString *url = [NSString stringWithFormat:@"%@?pageIndex=%d&pageSize=%d",URL_WalletTransactionList,pageIndex,pageSize];
    
    [self getWithUrl:url dict:nil success:success failure:failure];
}

//获取交易详情
+ (void)getTransactionDetailsWithCoinType:(NSString *)coinType txId:(NSString *)txId success:(successBlock)success failure:(errorBlock)failure {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSDictionary *dict = @{@"coinType":coinType,@"txId":txId};
    [params setObject:dict forKey:@"data"];
    [self getWithUrl:URL_TransactionDetails dict:params success:success failure:failure];
}


//POST
+ (void)postWithUrl:(NSString *)url dict:(NSDictionary *)dict success:(successBlock)success failure:(errorBlock)failure {
    
    DLog(@"params=%@",dict);
    DLog(@"POST请求URL = %@",url);
    
    //把登录token设置到请求头
    [WKHttpHeader sharedManager].manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *sessionToken = [[NSUserDefaults standardUserDefaults] valueForKey: @"sessionToken"];
    [[WKHttpHeader sharedManager].manager.requestSerializer setValue:sessionToken forHTTPHeaderField:@"Authorization"];
    
    [[WKHttpHeader sharedManager].manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"result=%@",result);
        success(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"error=%@",error);
        failure(error);
    }];
}

//GET
+ (void)getWithUrl:(NSString *)url dict:(NSDictionary *)dict success:(successBlock)success failure:(errorBlock)failure {
    
    DLog(@"params=%@",dict);
    DLog(@"GET请求URL = %@",url);
    
    //把登录token设置到请求头
    [WKHttpHeader sharedManager].manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *sessionToken = [[NSUserDefaults standardUserDefaults] valueForKey: @"sessionToken"];
    [[WKHttpHeader sharedManager].manager.requestSerializer setValue:sessionToken forHTTPHeaderField:@"Authorization"];
    
    [[WKHttpHeader sharedManager].manager GET:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"result=%@",result);
        success(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"error=%@",error);
        failure(error);
    }];
}

//DELETE
+ (void)deleteWithUrl:(NSString *)url dict:(NSDictionary *)dict success:(successBlock)success failure:(errorBlock)failure {
    //把登录token设置到请求头
    [WKHttpHeader sharedManager].manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *sessionToken = [[NSUserDefaults standardUserDefaults] valueForKey: @"sessionToken"];
    [[WKHttpHeader sharedManager].manager.requestSerializer setValue:sessionToken forHTTPHeaderField:@"Authorization"];
    
    [[WKHttpHeader sharedManager].manager DELETE:url parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"result=%@",result);
        success(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"error=%@",error);
        failure(error);
    }];
}

//PATCH
+ (void)patchWithUrl:(NSString *)url dict:(NSDictionary *)dict success:(successBlock)success failure:(errorBlock)failure {
    
    DLog(@"params=%@",dict);
    DLog(@"GET请求URL = %@",url);
    
    //把登录token设置到请求头
    [WKHttpHeader sharedManager].manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSString *sessionToken = [[NSUserDefaults standardUserDefaults] valueForKey: @"sessionToken"];
    [[WKHttpHeader sharedManager].manager.requestSerializer setValue:sessionToken forHTTPHeaderField:@"Authorization"];
    
    [[WKHttpHeader sharedManager].manager PATCH:url parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"result=%@",result);
        success(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"error=%@",error);
        failure(error);
    }];
}

@end
